package com.jingbian.cppccbackend.controller.admin;

import com.jingbian.cppccbackend.entity.HomePageNotifications;
import com.jingbian.cppccbackend.entity.Result;
import com.jingbian.cppccbackend.service.admin.HomePageNotificationsService;
import com.jingbian.cppccbackend.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 主页滚动通知表，用于存储滚动文字通知信息 前端控制器
 * </p>
 *
 * @author yezi
 * @since 2025-03-02
 */
@RestController
@RequestMapping("/admins/homePageNotifications")
public class adminHomePageNotificationsController {

    @Autowired
    private HomePageNotificationsService notificationsService;

    /**
     * 批量上传通知
     */
    @PostMapping("/batch")
    public Result<String> batchAddNotifications(@RequestBody List<HomePageNotifications> notifications) {
        boolean success = notificationsService.saveBatch(notifications);
        if (success) {
            return ResultUtil.success("通知批量上传成功");
        }
        return ResultUtil.fail("通知上传失败");
    }

    /**
     * 获取所有通知列表
     */
    @GetMapping("/list")
    public Result<List<HomePageNotifications>> getNotificationsList() {
        List<HomePageNotifications> list = notificationsService.list();
        return ResultUtil.success(list);
    }

    /**
     * 删除通知
     */
    @DeleteMapping("/{nid}")
    public Result<String> deleteNotification(@PathVariable Integer nid) {
        boolean success = notificationsService.removeById(nid);
        if (success) {
            return ResultUtil.success("通知删除成功");
        }
        return ResultUtil.fail("通知删除失败");
    }

    @GetMapping("/showInfo/{nid}")
    public Result<HomePageNotifications> showInfo(@PathVariable Integer nid) {
        HomePageNotifications notifications = notificationsService.getById(nid);
        return ResultUtil.success(notifications);
    }

    @PutMapping("/update")
    public Result<String> updateNotification(@RequestBody HomePageNotifications notifications) {
        boolean success = notificationsService.updateById(notifications);
        if (success) {
            return ResultUtil.success("通知更新成功");
        }else{
            return ResultUtil.fail("通知更新失败");
        }
    }
}
